import java.awt.*;
import java.util.Random;

class Toxin extends GameElement {
    // 黑斑位置缓存
    private final int[][] spots = new int[5][3]; // [x偏移, y偏移, 大小]

    public Toxin(float x, float y, int size, Color color, float effect, float duration) {
        super(x, y, size, color, effect, duration * 1.5f); // 持续时间更长
        this.floatSpeed *= 0.7f;
        Random rand = new Random();
        for (int i = 0; i < spots.length; i++) {
            spots[i][0] = rand.nextInt(size / 2) - size / 4; // x偏移 (-size/4 到 size/4)
            spots[i][1] = rand.nextInt(size / 2) - size / 4; // y偏移
            spots[i][2] = 2 + rand.nextInt(4);          // 斑点大小 (2-5)
        }// 移动速度更慢
    }

    @Override
    public boolean update() {
        // 更慢但更精确的索敌
        Embryo embryo = GameState.getInstance().getEmbryo();
        float dx = embryo.getX() - x;
        float dy = embryo.getY() - y;
        float dist = (float) Math.sqrt(dx * dx + dy * dy);

        if (dist > 0) {
            // 40%速度向胚胎移动，但更稳定
            float seekForce = 0.4f;
            x += (dx / dist) * floatSpeed * seekForce;
            y += (dy / dist) * floatSpeed * seekForce;
        }

        // 保留基本漂浮行为
        return super.update();
    }


    @Override
    public void draw(Graphics2D g) {
        // 绘制红色基底
        float alpha = 1 - age / duration;
        g.setColor(new Color(
                color.getRed(),
                color.getGreen(),
                color.getBlue(),
                (int) (alpha * 200 + 55)
        ));
        g.fillOval((int) x - size / 2, (int) y - size / 2, size, size);

        // 绘制黑色斑点
        g.setColor(new Color(0, 0, 0, (int) (alpha * 220 + 35)));
        for (int[] spot : spots) {
            g.fillOval(
                    (int) (x + spot[0] - (float) spot[2] / 2),
                    (int) (y + spot[1] - (float) spot[2] / 2),
                    spot[2], spot[2]
            );
        }
    }


    public boolean effect(Embryo embryo, GameState state) {
        // 造成70%减速，持续5秒
        embryo.applySpeedModifier(0.3f, 5f);
        // 附加基础伤害
        state.updateHealth(effectValue);
        return true; // 碰撞后消失
    }
}
